/**
 *@Description 订单首页
 *@Author zhangnf
 *@Date 2015-09-23
 */
var orderCtrl  = new XFApp.client();
XFApp.client.prototype.order = {
	successCode : "10000",
	page : null,
	dataLst : null,
	urlItems : {
		queryByPageURL : basePath+"/page",
		sendURL : basePath+"/send"
	},
	queryPage : function(){
		_this = this;
		if(!!_this.page){
			_this.page.page("remote",0,$('#searchForm').serialize());
		}else{
		_this.page = $("#page").page({
			pageSize : 10,
			remote: {
				url: _this.urlItems.queryByPageURL,
		        params: $('#searchForm').serialize(), 
		        success: function (data, pageIndex) {
		        	if(data.code==_this.successCode){
		        		var pageResult = data.result;
		        		if(null == data 
								|| null == data.result 
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
		        		}else{
		        			dataLst = pageResult.itemList;
		        			for(var index in pageResult.itemList){
		        				var element = pageResult.itemList[index];
		        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
		        			}
		        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
		        			_this.checkSendBtn(pageResult.itemList);
		        		}
		        	}else{
		        		window.webDialogUtil.alert("提示","操作失败");
		        	}
		        },
		        error : function(){
		        	window.webDialogUtil.alert("提示","操作失败");
		        }
			},
			firstBtnText : '首页',
			lastBtnText : '尾页',
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showJump : true,
			jumpBtnText : '跳转',
			showPageSizes : true,
			infoFormat : '共{pageCount}页,{total}项'
		});
		}
	},
	checkSendBtn : function(dataLst){
		_this = this;
		var _index = 0;
		var _btnNum = 0;
		$("#listTable table tr").each(function() {
			$(this).children("td:eq(3)").each(function() {
				$(this).children("a").each(function() {
					if(_this.isSend($(this).attr("dataVal"),dataLst[_index-1])
							&& 0 != _btnNum){
						$(this).attr("disabled","disabled");
						$(this).removeAttr('onclick');
					}
					_btnNum++;
				});
			});
			_btnNum = 1;
			_index ++;
		});
	},
	isSend : function(returnStatus,statusArray){
		if(null == statusArray.cebReceives){
			return false;
		}
		var _array = statusArray.cebReceives.split(",");
		for(val in _array){
			if(returnStatus == _array[val]){
				return true;
			}
		}
		return false;
	},
	detailDialog : function(index){
		_this = this;
		var content = dataLst[index-1].cebBody;
		$("#txtArea").val(content);
		webAlert({
            title:'详细信息',
            content:document.getElementById("cebContent"),
            opacity:.5,
            bg:"gray",
            width:300,
            height:10
            });
	},
	send : function(id,cebVal,index,btnIndex){
		_this = this;
		$.ajax({
		    url:_this.urlItems.sendURL,
		    data:{id:id,cebVal:cebVal},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	window.webDialogUtil.alert("提示",data.msg);
		    	if(true == data.isSuccess){
		    		//灰掉按钮
		    		_this.disableBtn(index,cebVal,btnIndex);
		    	}
		     },  
		     error : function() {
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	disableBtn : function(index,cebVal,btnIndex){
		var _index = 0;
		$("#listTable table tr").each(function() {
			if(_index == index){
				$(this).children("td:eq(3)").each(function() {
					$(this).children("a:eq("+btnIndex+")").attr("disabled","disabled");
					$(this).children("a:eq("+btnIndex+")").removeAttr('onclick');
				});
			}
			_index ++;
		});
	},
	dataFormat : function(timeLong){
		if(null == timeLong
				|| "" == timeLong){
			return "";
		}
		return new Date(timeLong).format("yyyy-MM-dd hh:mm:ss")
	},
	event : function(){
		_this = this;
		
		$("#searchButton").on("click",function(){
			orderCtrl.order.queryPage();
		});
		
		orderCtrl.order.queryPage();
	},
};
$.views.converters({
	ftime : function(value) {
		return orderCtrl.order.dataFormat(value) ;
	},
	fdkstatus : function(value){
		return clientIdCtrl.clientId.get(value);
	},
	fcebType : function(value){
		switch (value){
		case 501 : return "运单申报";
		case 503 : return "运抵申报";
		}
	}
});
$(function(){
	orderCtrl.order.event();
});